package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.dao.UserMapper;
import com.qf.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 千锋健哥
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;


    @Override
    public List<User> findAll() {
        List<User> list = userMapper.selectList(null);
        return list;
    }

    @Override
    public void insertUser(User user) {
        userMapper.insert(user);
    }

    @Override
    public List<User> findByNameAndSex(String name, String sex) {
        //创建查询条件对象
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        //根据名字模糊查询
        //第一参数: 判断变量不为空再进行查询
        //第二参数: 字段名
        //第三参数: 字段值
        queryWrapper.like(name != null, "name", name);

        //根据性别精确查询
        queryWrapper.eq(sex != null, "sex", sex);

        //根据id降序排序
        queryWrapper.orderByDesc("id");

        //根据条件查询
        List<User> list = userMapper.selectList(queryWrapper);
        return list;
    }

}
